package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.AndroidApprovalRecord;
import com.sdry.model.hpf.ApprovalRecord;
import com.sdry.model.hpf.Page;
import com.sdry.model.llm.User;
import com.sdry.service.hpf.ApprovalRecordService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: ApprovalRecordController 
 * @Description:审批记录Controller
 * @Author hpf
 * @Date 2018年12月10日 上午11:12:17 
 * @version 1.0
 */
@RequestMapping(value="/approvalRecord")
@Controller
public class ApprovalRecordController {

	@Resource
	private ApprovalRecordService approvalRecordService;
	
	/**
	 * 分页查询待办
	 * @param page
	 * @param approvalRecord
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageApprovalRecord")
	public void listPageApprovalRecord(Page page,ApprovalRecord approvalRecord,HttpServletResponse response,HttpSession session) throws Exception{
		User user = (User) session.getAttribute("currentUser");
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		map.put("approverID", user.getUid());
		int count = approvalRecordService.countApprovalRecord(user.getUid());
		List<ApprovalRecord> approvalRecordList = approvalRecordService.listPageApprovalRecord(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(approvalRecordList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 分页查询已办
	 * @param page
	 * @param approvalRecord
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listPageApprovalComplete")
	public void listPageApprovalComplete(Page page,ApprovalRecord approvalRecord,HttpServletResponse response,HttpSession session) throws Exception{
		User user = (User) session.getAttribute("currentUser");
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		map.put("approverID", user.getUid());
		int count = approvalRecordService.countApprovalRecord(user.getUid());
		List<ApprovalRecord> approvalRecordList = approvalRecordService.listPageApprovalComplete(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(approvalRecordList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存
	 * @param approvalRecord
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/saveApprovalRecord")
	public void saveApprovalRecord(ApprovalRecord approvalRecord,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (approvalRecord.getArID() == null || "".equals(approvalRecord.getArID())) {
			isSuccess = approvalRecordService.insertApprovalRecord(approvalRecord);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 查询所有
	 * @param pickID
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllApprovalRecord")
	public void listAllApprovalRecord(Long pickID,HttpServletResponse response) throws Exception{
		List<ApprovalRecord> approvalRecordList = approvalRecordService.listAllApprovalRecord(pickID);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(approvalRecordList);
		 jsonObject.put("code", 0);
         jsonObject.put("data", jsonArray);
         jsonObject.put("count", 10);
         ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 安卓查询所有待办
	 * @param approverID
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllAndroidApprovalRecord")
	public void listAllAndroidApprovalRecord(String approverID,HttpServletResponse response) throws Exception{
		List<AndroidApprovalRecord> approvalRecordList = approvalRecordService.listAllAndroidApprovalRecord(Long.parseLong(approverID));
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(approvalRecordList);
		try {
			if(approvalRecordList.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
